package com.zhoug.zmui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RotateDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ProgressBar;

import com.zhoug.common.utils.LogUtils;

/**
 * loadView
 *zmui_style_ZMUILoadView
 * @Author 35574
 * @Date 2020/7/3
 * @Description
 */
public class ZMUILoadView extends ProgressBar {
    private static final String TAG = ">>>ZMUILoadView";
    private static final boolean DEBUG = false;
    private int loadViewSize;

    public ZMUILoadView(Context context) {
        super(context);
        init(context, null);
    }

    public ZMUILoadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);

    }

    public ZMUILoadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ZMUILoadView, R.attr.zmui_style_ZMUILoadView, R.style.zmui_style_ZMUILoadView);
        loadViewSize = typedArray.getDimensionPixelOffset(R.styleable.ZMUILoadView_zmui_loadView_size, 0);
        Drawable loadViewDrawable = typedArray.getDrawable(R.styleable.ZMUILoadView_zmui_loadView_image);
        typedArray.recycle();
        if (DEBUG) {
            LogUtils.d(TAG, "init:loadViewSize=" + loadViewSize);
            LogUtils.d(TAG, "init:loadViewDrawable=" + loadViewDrawable);
        }
        setLoadViewImage(loadViewDrawable);
    }

    /**
     * 设置loadView图片可以为 RotateDrawable,BitMapDrawable
     *
     * @param drawable
     */
    public void setLoadViewImage(Drawable drawable) {
        if (drawable != null) {
            if (drawable instanceof RotateDrawable) {
                setIndeterminateDrawable(drawable);
            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    RotateDrawable rotateDrawable = new RotateDrawable();
                    rotateDrawable.setPivotX(0.5f);
                    rotateDrawable.setPivotY(0.5f);
                    rotateDrawable.setDrawable(drawable);
                    rotateDrawable.setFromDegrees(0);
                    rotateDrawable.setToDegrees(1440);
                    setIndeterminateDrawable(rotateDrawable);
                }
            }
        }
    }

    public void setLoadViewSize(int loadViewSize) {
        if (loadViewSize > 0) {
            this.loadViewSize = loadViewSize;
        }
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY) {
            width = loadViewSize+getPaddingStart()+getPaddingEnd();
        }
        if (MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY) {
            height = loadViewSize+getPaddingTop()+getPaddingBottom();
        }
        if(DEBUG){
            LogUtils.d(TAG, "onMeasure:width="+width);
            LogUtils.d(TAG, "onMeasure:height="+height);
        }
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }
}
